#include "parkingapplywidget.h"
#include "ui_parkingapplywidget.h"
#include <QDebug>
#include <QSqlQuery>
#include "loginwidget.h"
#include <QMessageBox>
#include <QSqlError>
#include <QDateTime>

ParkingApplyWidget::ParkingApplyWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ParkingApplyWidget)
{
    ui->setupUi(this);
    initWindow();
}

ParkingApplyWidget::~ParkingApplyWidget()
{
    delete ui;
}

/**
 * @brief 初始化窗体
 */
void ParkingApplyWidget::initWindow()
{
    this->setWindowTitle("车位申请");

    ui->cb_type->addItem("地上");
    ui->cb_type->addItem("楼层");
    ui->cb_can_charge->addItem("不可充电");
    ui->cb_can_charge->addItem("可充电");
    ui->cb_size->addItem("小");
    ui->cb_size->addItem("大");
}

/**
 * @brief 点击申请
 */
void ParkingApplyWidget::on_btn_apply_clicked()
{
    QString type = ui->cb_type->currentText();
    QString canCharge = ui->cb_can_charge->currentText();
    QString size = ui->cb_size->currentText();
    QString carNum = ui->edit_car_num->text();
    qDebug() << "车位类型：" << type
             << "\n车位可充电：" << canCharge
             << "\n车位大小：" << size
             << "\n车牌号：" << carNum;

    //查询符合条件的可用车位
    QSqlQuery query;
    query.prepare("SELECT position FROM parking WHERE status='可用' AND type=:type AND can_charge=:can_charge AND size = :size"); //查询所有空的车位
    query.bindValue(":type", type);
    query.bindValue(":can_charge", canCharge);
    query.bindValue(":size", size);
    if(!query.exec())
    {
        qDebug() << query.lastError();
    }
    else
    {
        if(query.next())
        {
            QString position = query.value(0).toString();

            //更新车位信息为已用
            QSqlQuery query2;
            query2.prepare("UPDATE parking SET status='已用' WHERE position = :position");
            query2.bindValue(":position", position);
            query2.exec();
            //插入车位申请信息
            QSqlQuery query3;
            query3.prepare("INSERT INTO parking_apply(apply_username, apply_position, apply_type, apply_can_charge, apply_size, car_num, is_pay) \
                          VALUES(:apply_username, :apply_position, :apply_type, :apply_can_charge, :apply_size, :car_num, '未缴费')");
            query3.bindValue(":apply_username", LoginWidget::userName);
            query3.bindValue(":apply_position", position);
            query3.bindValue(":apply_type", type);
            query3.bindValue(":apply_can_charge", canCharge);
            query3.bindValue(":apply_size", size);
            query3.bindValue(":car_num", carNum);
            if(!query3.exec())
            {
                qDebug()<<query3.lastError();
                QMessageBox::warning(this, tr("错误"), tr("申请失败"), QMessageBox::Ok);
            }
            else
            {
                QMessageBox::information(this, tr("提示"), QString("申请车位成功，您的车位位置为%1,请及时缴费").arg(position));
            }
            //查询刚刚插入的车位申请信息的id
            query.prepare("SELECT id FROM parking_apply ORDER BY id DESC");
            query.exec();
            query.next();
            int record_id = query.value(0).toInt();

            //生成缴费记录
            QSqlQuery query;
            query.prepare("INSERT INTO pay_record (pay_username, pay_type, is_pay, pay_datetime, record_id) "
                          "VALUES(:pay_username, '车位费', '未缴费', :pay_datetime, :record_id)");
            query.bindValue(":pay_username", LoginWidget::userName);
            query.bindValue(":pay_datetime", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
            query.bindValue(":record_id", record_id);
            query.exec();
        }
        else
        {
            QMessageBox::information(this, tr("提示"), tr("暂无符合的车位"));
        }
    }

}
